using AutoMapper;
using MediatR;
using SmartApplication.Api.Applient.Command.Feed;
using SmartApplication.Api.Dto;
using SmartApplication.ApiCode;
using SmartApplication.Domain.Entity;
using SmartApplication.Infrastructure.Instrument;

namespace SmartApplication.Api.Applient.CommandHandler.Feed
{
    public class GetAllCompaniesHandler : IRequestHandler<GetAllCompaniesCommand, ApiResult<List<CompaniesDto>>>
    {
        public GetAllCompaniesHandler(IBaseRepository<CompaniesModel> companRepository, IMapper mapper)
        {
            CompanRepository = companRepository;
            Mapper = mapper;
        }

        public IBaseRepository<CompaniesModel> CompanRepository { get; }
        public IMapper Mapper { get; }

        /// <summary>
        /// 显示所有养殖企业信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ApiResult<List<CompaniesDto>>> Handle(GetAllCompaniesCommand request, CancellationToken cancellationToken)
        {
            var res = new ApiResult<List<CompaniesDto>>();

            IQueryable<CompaniesModel>? companies = CompanRepository.GetAll();
            List<CompaniesDto>? mapper = Mapper.Map<List<CompaniesDto>>(companies);

            res.Message = "显示所有公司成功";
            res.Code = EnumCode.Success;
            res.Data = mapper;
            return res;
        }
    }
}